<template>
  <el-form ref="ruleForm" :model="countParam" :rules="rules" label-width="160px">
    <el-row>
      <el-col :span="11">
        <el-form-item label="污染损害发生年" prop="start">
          <el-select
            v-model="countParam.start"
            clearable
            filterable
            allow-create
            @change="fillTable"
          >
            <el-option
              v-for="item in years"
              :key="item.year"
              :label="item.label"
              :value="item.year"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="恢复至标准基准年" prop="end">
          <el-select v-model="countParam.end" clearable filterable allow-create @change="fillTable">
            <el-option
              v-for="item in years"
              :key="item.year"
              :label="item.label"
              :value="item.year"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="现值系数(r:范围0~1)" prop="r">
          <el-input v-model="countParam.r" clearable />
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="基准年(T)" prop="reference">
          <el-select v-model="countParam.reference" clearable filterable allow-create>
            <el-option
              v-for="item in years"
              :key="item.year"
              :label="item.label"
              :value="item.year"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="含水层体积Rt" prop="damage">
          <el-input v-model="countParam.damage" clearable />
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="恢复时间(年)">
          <el-input :value="countParam.count" clearable :disabled="true" />
        </el-form-item>
      </el-col>
    </el-row>
    <div>
      <div class="text-right mb-16px">
        <el-button type="primary" plain :disabled="!btnVisible" @click="openUploadValueDialog">
          导入损害程度数据
        </el-button>
      </div>
      <el-table :data="ndshValues" :border="true" stripe height="300px">
        <el-table-column type="index" align="left" width="80" />
        <el-table-column prop="name" label="年份" align="left" />
        <el-table-column prop="value" label="损害程度(d:数值范围0~1)">
          <template #default="scope">
            <el-input v-model="scope.row.value" />
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-row style="margin-top: 10px">
      <el-col :span="21">
        <h5>补偿性恢复行动的单位效益(E)</h5>
      </el-col>
    </el-row>
    <el-row style="margin-top: 10px">
      <el-col :span="11">
        <el-form-item label="补偿性恢复行动单位效益-起始年(t1)" label-width="250px" prop="tStart">
          <el-select v-model="countParam.tStart" clearable filterable allow-create>
            <template v-for="item in years" :key="item.year">
              <el-option :label="item.label" :value="item.year" />
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="贴现时间(年)" prop="tTime">
          <el-input v-model="countParam.tTime" clearable />
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="贴现系数(r:数值范围0~1)" label-width="250px" prop="tr">
          <el-input v-model="countParam.tr" clearable />
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="基准年(T)" prop="tRef">
          <el-select v-model="countParam.tRef" clearable filterable allow-create>
            <el-option
              v-for="item in years"
              :key="item.year"
              :label="item.label"
              :value="item.year"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="补偿性行动年度单位效益(e)" label-width="250px">
          <el-input :value="1" />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <div class="text-right">
      <el-space :size="20">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="submitForm()"> 确定 </el-button>
      </el-space>
    </div> -->
  </el-form>
  <equivalentResult
    :add-dialog-visible="dialogVisible"
    :results="results"
    @close-dialog="dialogVisible = false"
  />
  <uploadValueDialog
    :dialogVisible="uploadVisible"
    @close-dialog="uploadVisible = false"
    @submit-handler="uploadValueSubmitHandler"
  />
</template>
<script setup lang="ts">
import * as _ from 'lodash-es'
import { ElMessage } from 'element-plus'
import equivalentResult from '@/components/forestDamage/equivalentResult.vue'
import uploadValueDialog from '@/components/forestDamage/uploadValueDialog.vue'
import { quantization } from '@/utils/quantization'

defineOptions({ name: 'Quantization' })

interface INdsh {
  name: string
  value: number
}

const dialogVisible = ref(false)
const btnVisible = ref(false)
const uploadVisible = ref(false)

// 通过getFullYear()方法获取当前年份
const currentYear = new Date().getFullYear()
const param = {
  start: currentYear, // 起始年
  end: undefined, // 结束年
  r: undefined, // 现值系数系数
  reference: currentYear, // 基准年
  damage: undefined, // 含水层体积
  count: undefined, // 恢复时间
  tStart: currentYear,
  tr: undefined,
  tRef: currentYear,
  tTime: 200
}

const countParam: any = reactive<any>(param)

const rules = reactive({
  start: [{ required: true, message: '请输入', trigger: 'change' }],
  end: [{ required: true, message: '请选择', trigger: 'change' }],
  r: [
    { required: true, message: '请输入', trigger: 'blur' },
    { pattern: /^\d+(\.\d+)?$/, message: '格式不正确' }
  ],
  reference: [{ required: true, message: '请输入', trigger: 'change' }],
  damage: [
    { required: true, message: '请输入', trigger: 'blur' },
    { pattern: /^\d+(\.\d+)?$/, message: '格式不正确' }
  ],
  count: [
    { required: true, message: '请输入', trigger: 'blur' },
    { pattern: /^\d+(\.\d+)?$/, message: '格式不正确' }
  ],
  tStart: [{ required: true, message: '请输入', trigger: 'change' }],
  tr: [
    { required: true, message: '请输入', trigger: 'blur' },
    { pattern: /^\d+(\.\d+)?$/, message: '格式不正确' }
  ],
  tRef: [{ required: true, message: '请输入', trigger: 'change' }],
  tTime: [
    { required: true, message: '请输入', trigger: 'blur' },
    { pattern: /^\d+(\.\d+)?$/, message: '格式不正确' }
  ]
})

const years = reactive<any[]>([])
const initOpeions = function () {
  for (let i = 1990; i < 2501; i++) {
    years.push({
      label: i + '年',
      year: i
    })
  }
}
initOpeions()

let ndshValues: INdsh[] = reactive<INdsh[]>([])

const fillTable = function () {
  if (countParam.start == '' || countParam.end == '') {
    btnVisible.value = false
    ndshValues.length = 0
    return
  }
  ndshValues.length = 0

  let T_t: number = countParam.end - countParam.start + 1
  if (T_t <= 0) {
    btnVisible.value = false
  } else {
    btnVisible.value = true
  }
  for (let i = 0; i < T_t; i++) {
    ndshValues.push({
      name: Number(countParam.start) + i + '',
      value: 0
    })
  }
}

let results = ref<any[]>([])
const ruleForm = ref()
const submitForm = async () => {
  await ruleForm.value.validate()
  const { H, E, S } = quantization({
    ...countParam,
    list: ndshValues
  })
  return { H, E, S }
  //   dialogVisible.value = true

  //   results.value = [
  //     {
  //       name: '        本项目的期间损害总计为： ',
  //       value: H.toFixed(4) + ' m3*贴现年'
  //     },
  //     {
  //       name: '        补偿性恢复行动的单位效益为： ',
  //       value: E.toFixed(4) + '  贴现年'
  //     },
  //     {
  //       name: '补偿性恢复规模(S)： ',
  //       value: '        S = H / E '
  //     },
  //     {
  //       name: '        本项目需要补偿性恢复的体积为： ',
  //       value: S.toFixed(4) + '  m3'
  //     }
  //   ]
}
// 暴漏给父组件
defineExpose({ submitForm })

const resetForm = () => {
  ruleForm.value.resetFields()
}

const openUploadValueDialog = function () {
  uploadVisible.value = true
}

const uploadValueSubmitHandler = function (content: string) {
  if (content.length > 0) {
    let valueArr = content.split('\n')
    if (valueArr.length > 0) {
      let dic1 = {}
      for (let i = 0; i < valueArr.length; i++) {
        if (valueArr[i] != null) {
          let nvarr = valueArr[i].split(',')
          if (nvarr.length > 1) {
            dic1[nvarr[0]] = nvarr[1]
          }
        }
      }
      for (let i = 0; i < ndshValues.length; i++) {
        if (dic1[ndshValues[i].name] != null) {
          ndshValues[i].value = Number(dic1[ndshValues[i].name])
        }
      }
    }
  }
}
</script>
